package com.acme.bankapp.service.bank.builder;

import java.util.Map;

import com.acme.bankapp.domain.bank.Client;
import com.acme.bankapp.domain.bank.Gender;

/**
 * Builds client from properties map.
 * 
 */
public class ClientBuilder extends AbstractBuilder<Client> {
	private static final String NAME_FIELD = "name";
	private static final String GENDER_FIELD = "gender";

	private String name = null;
	private Gender gender = null;

	private void setName(String name) throws BuildingFailedException {
		validateNotNull(name);
		this.name = name;
	}

	private void setGender(String gender) throws BuildingFailedException {
		validateNotNull(gender);
		switch (gender) {
		case "m":
			this.gender = Gender.MALE;
			break;
		case "f":
			this.gender = Gender.FEMALE;
			break;
		default:
			throw new BuildingFailedException();
		}
	}

	/**
	 * Builds new client from properties or throws exception on failure.
	 * 
	 * @param properties
	 *            map of properties
	 * @return newly created client
	 * @throws BuildingFailedException
	 */
	public Client buildFromProperties(Map<String, String> properties)
			throws BuildingFailedException {
		setName(properties.get(NAME_FIELD));
		setGender(properties.get(GENDER_FIELD));
		return new Client(gender, name);
	}
}
